package com.lottery.im.module.chat.ui;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import com.lottery.im.R;
import com.lottery.im.api.result.entity.SingleRedPacketsReceived;
import com.lottery.im.base.BaseFragmentActivity;
import com.lottery.im.base.BaseSwipeFragment;
import com.lottery.im.module.login.ui.ForgetPasswordFragment;
import com.lottery.im.module.setting.ui.AddMoneyPwdFragment;
import com.lottery.im.module.setting.ui.ProfileFragment;
import com.tencent.imsdk.TIMConversationType;

/**
 * 新界面 (嵌套fragment使用)
 */
public class NewFragmentActivity extends BaseFragmentActivity {

  private final static String TYPE_KEY = "TYPE_KEY";
  private final static String ID_KEY = "ID_KEY";
  private final static String RED_TYPE_KEY = "RED_TYPE_KEY";
  private final static String REDPACKETS_KEY = "REDPACKETS_KEY";
  private final static String NOTE_KEY = "NOTE_KEY";

  public static void createSelfProfile(Context context) {
    Intent intent = new Intent(context, NewFragmentActivity.class);
    intent.putExtra(TYPE_KEY, FragmentInfoType.Self);
    context.startActivity(intent);
  }

  public static void create(Context context, FragmentInfoType type, String id) {
    Intent intent = new Intent(context, NewFragmentActivity.class);
    intent.putExtra(TYPE_KEY, type);
    intent.putExtra(ID_KEY, id);
    context.startActivity(intent);
  }

  public static void create(Context context, TIMConversationType type,
      SingleRedPacketsReceived redPacketsReceived,String note) {
    Intent intent = new Intent(context, NewFragmentActivity.class);
    intent.putExtra(TYPE_KEY, FragmentInfoType.RedPacketsDetail);
    intent.putExtra(RED_TYPE_KEY, type);
    intent.putExtra(REDPACKETS_KEY, redPacketsReceived);
    intent.putExtra(NOTE_KEY, note);
    context.startActivity(intent);
  }


  public static void create(Context context, FragmentInfoType type) {
    Intent intent = new Intent(context, NewFragmentActivity.class);
    intent.putExtra(TYPE_KEY, type);
    context.startActivity(intent);
  }

  @Override protected int getContextViewId() {
    return R.id.activity_profile;
  }

  @Override protected void initPresenter() {

  }

  @Override protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    if (savedInstanceState == null) {

      BaseSwipeFragment fragment;
      FragmentInfoType type = (FragmentInfoType) getIntent().getSerializableExtra(TYPE_KEY);
      String id = getIntent().getStringExtra(ID_KEY);
      switch (type) {
        case Self:
          fragment = new ProfileFragment();
          break;
        case Other:
          fragment = UserProfileFragment.create(id);
          break;
        case Group:
          fragment = GroupProfileFragment.create(id);
          break;
        case RedPacketsDetail:
          TIMConversationType timType =
              (TIMConversationType) getIntent().getSerializableExtra(RED_TYPE_KEY);
          SingleRedPacketsReceived redPacketsReceived =
              (SingleRedPacketsReceived) getIntent().getSerializableExtra(REDPACKETS_KEY);
          String note = getIntent().getStringExtra(NOTE_KEY);
          fragment = RedPacketsDetailFragment.create(timType, redPacketsReceived,note);
          break;
        case RedPacketsRecord:
          fragment = new RedPacketsRecordFragment();
          break;
        case SETMONEYPWD:
          fragment = new AddMoneyPwdFragment();
          break;
        case FORGETMONEYPWD:
          fragment = ForgetPasswordFragment.create(2);
          break;
        default:
          fragment = new ProfileFragment();
          break;
      }

      getSupportFragmentManager()
          .beginTransaction()
          .add(getContextViewId(), fragment, fragment.getClass().getSimpleName())
          .addToBackStack(fragment.getClass().getSimpleName())
          .commit();
    }
  }

  /**
   * 打开对应的fragment
   */
  public enum FragmentInfoType {
    Self, //自己资料
    Other,//他人资料
    Group,//群资料
    RedPacketsDetail, //红包详情
    RedPacketsRecord, //红包记录
    SETMONEYPWD,       //设置资金密码
    FORGETMONEYPWD      //忘记资金密码
  }
}
